﻿using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using System;
using System.ComponentModel;
using System.IO;
using System.Windows.Forms;

namespace XLSranker
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            textBox2.Text = "请按“打开文件”按钮选择要处理的文件";

            backgroundWorker1 = new BackgroundWorker(); //执行比较串号任务的worker
            backgroundWorker1.WorkerReportsProgress = true;
            backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
            backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
            backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);

            backgroundWorker2 = new BackgroundWorker(); //执行删除串号任务的worker
            backgroundWorker2.WorkerReportsProgress = true;
            backgroundWorker2.DoWork += new DoWorkEventHandler(backgroundWorker2_DoWork);
            backgroundWorker2.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker2_RunWorkerCompleted);
            backgroundWorker2.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker2_ProgressChanged);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog file1 = new OpenFileDialog();
            if (file1.ShowDialog() == DialogResult.OK) 
            {
                textBox1.Text = file1.FileName; //获取文件路径和文件名

                FileStream fs = File.OpenRead(textBox1.Text); //从文件路径获取文件流
                IWorkbook workbook = new HSSFWorkbook(fs);
                fs.Close();
                ISheet sheet = workbook.GetSheetAt(0);
                int length = sheet.GetRow(0).GetCell(0).ToString().Length;//取得首个单元格串号长度

                string st = "当前打开的文件包含位串号，按“开始”按钮开始处理";
                textBox2.Text = st.Insert(9, length.ToString()); //在程序界面显示首个单元格串号位数

                button2.Enabled = true;
            }


            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            button2.Enabled = false;
            textBox2.Text = "正在整理数据......请稍候......";
            FileStream fs = File.OpenRead(textBox1.Text); //从文件路径获取文件流
            IWorkbook workbook = new HSSFWorkbook(fs);
            fs.Close();

            backgroundWorker1.RunWorkerAsync(workbook); //在后台执行删除行操作
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {

            IWorkbook workbook = e.Argument as IWorkbook;
            CompareRow.compareRow(workbook, backgroundWorker1);
            e.Result = workbook;
        }

        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            int num = (int)e.ProgressPercentage;
            progressBar1.Value = num;
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            IWorkbook workbook = e.Result as IWorkbook;
            progressBar1.Value = 0;
            textBox2.Text = "正在删除空行......请稍候......";
            backgroundWorker2.RunWorkerAsync(workbook);
        }

        private void backgroundWorker2_DoWork(object sender, DoWorkEventArgs e)
        {            
            IWorkbook workbook = e.Argument as IWorkbook;
            DeleteRow.deleteRow(workbook, backgroundWorker2);
            e.Result = workbook;
        }

        private void backgroundWorker2_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            int num = (int)e.ProgressPercentage;
            progressBar1.Value = num;
        }

        private void backgroundWorker2_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            IWorkbook workbook = e.Result as IWorkbook;
            //FileStream fs2 = File.Create(@"C:\Users\GaoGuohua\Desktop\Result.xls");
            FileStream fs2 = File.Create(textBox1.Text.Insert(textBox1.Text.IndexOf(".xls"), "_整理后"));
            workbook.Write(fs2);
            fs2.Close();
            textBox2.Text = "处理完成，文件保存到源文件同文件夹";
            MessageBox.Show("完成，感谢使用本软件");
        }

        private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
        {
            //测试主分支
        }        
    }
}
